import axios from "axios";
import { Effect, Reducer } from "umi";

const STATE = {
	list: [] as ItemType[],
};

interface DocType<T = typeof STATE> {
	namespace: "docs";
	state: T;
	reducers: {
		setList: Reducer<T>;
	};
	effects: {
		getDate: Effect;
	};
}

export interface ItemType {
	userId: number;
	id: number;
	title: string;
	body: string;
}

const docs: DocType = {
	namespace: "docs",
	state: STATE,
	reducers: {
		setList(_, { payload }) {
			return { list: payload.data };
		},
	},
	effects: {
		*getDate(_, { call, put }) {
			try {
				const { data } = yield call(() =>
					axios.get("http://jsonplaceholder.typicode.com/posts")
				);
				yield put({ type: "setList", payload: { data } });
			} catch (error) {
				console.log(error, "docs--error");
			}
		},
	},
};

export default docs;
